from game import *

class Dynamic(pygame.sprite.Sprite):

	def __init__(self, game, image_path, pos):
		pygame.sprite.Sprite.__init__(self)
		self.game = game
		self.dbg = False

		# Sprite
		if globals.images.has_key(image_path):
			self.image = globals.images[image_path]
		else:
			self.image = pygame.image.load(os.path.join('data', image_path))
			globals.images[image_path] = self.image
		
		self.image = self.image.convert_alpha()
		
		self.flip = False
		self.rect = self.image.get_rect()

		# Physics
		self.pos = pos 				# position
		self.acc = Vector()		# accelleration
		self.vel = Vector()   # velocity
		self.mvel = Vector()	# max velocity
		self.ang = 0.0				# angle
		self.ave = 0.0				# angular velocity
		self.dam = 1.0				# damping

	def update(self):
	
		self.ang += self.ave
		
		self.vel += self.acc
		self.vel *= self.dam
		
		if self.vel.x > self.mvel.x:
			self.vel.x = self.mvel.x
		elif self.vel.x < -self.mvel.x:
			self.vel.x = -self.mvel.x
		if self.vel.y > self.mvel.y:
			self.vel.y = self.mvel.y
		elif self.vel.y < -self.mvel.y:
			self.vel.y = -self.mvel.y
		
		self.pos += self.vel
		self.acc = Vector()
		
	def apply_force(self, force):
		self.acc = force
		

	def draw(self):
		if self.flip:
			image = pygame.transform.rotate(self.image, -self.ang)
			image = pygame.transform.flip(image, False, True)
		else:
			image = pygame.transform.rotate(self.image, self.ang)

		rect = image.get_rect()
		rect.center = self.game.camera.translate(self.pos).to_list()

		self.game.screen.blit(image, rect.topleft)

